package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.ChatMessage;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.IChatMessageService;
import cn.wolfcode.trip.base.service.IDynamicService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注册用户资源控制器
 */

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IChatMessageService chatMessageService;

    @Autowired
    private IDynamicService dynamicService;

    @PostMapping
    public JsonResult register(User user){
        JsonResult json = new JsonResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @PutMapping("{id}")
    public JsonResult update(User user){
        JsonResult json = new JsonResult();
        try {
            userService.update(user);
            json.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("{id}")
    public User selectById(@PathVariable Long id){
        User user = userService.selectById(id);
        return user;
    }

    @GetMapping("/{authorId}/travels")
    public PageInfo query(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }

    //查询分类的全部收藏
    @GetMapping("typies")
    public PageInfo query(CollectionQueryObject qo) {
        //调用业务方法
        return userService.queryForCollections(qo);
    }

    //查询单个游记是否被该用户收藏
    @GetMapping("/travesCollection/{travelId}")
    public JsonResult getByTravelId(@PathVariable Long travelId) {
        JsonResult jsonResult = new JsonResult();
        try {
            userService.getByTravelId(travelId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //查询单个攻略是否被该用户收藏
    @GetMapping("/strategyCollection/{strategyId}")
    public JsonResult getBystrategyId(@PathVariable Long strategyId) {
        JsonResult jsonResult = new JsonResult();
        try {
            userService.getBystrategyId(strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //查询单个攻略文章是否被该用户收藏
    @GetMapping("/strategyDetailCollection/{strategyDetailId}")
    public JsonResult getBystrategyDetailId(@PathVariable Long strategyDetailId) {
        JsonResult jsonResult = new JsonResult();
        try {
            userService.getBystrategyDetailId(strategyDetailId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //修改单个攻略文章收藏状态
    @PutMapping("/changeStrategyDetailCollection/{strategyDetailId}")
    public JsonResult changeStrategyDetailCollection(@PathVariable Long strategyDetailId, int type) {
        JsonResult jsonResult = new JsonResult();
        userService.changeStrategyDetailCollection(strategyDetailId, type);
        return jsonResult;
    }

    //查询单个日报是否被该用户收藏
    @GetMapping("/dailyCollection/{dailyId}")
    public JsonResult getBydailyId(@PathVariable Long dailyId) {
        JsonResult jsonResult = new JsonResult();
        try {
            userService.getBydailyId(dailyId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //修改单个日报收藏状态
    @PutMapping("/changeDailyCollection/{dailyId}")
    public JsonResult changeDailyCollection(@PathVariable Long dailyId, int type) {
        JsonResult jsonResult = new JsonResult();
        userService.changeDailyCollection(dailyId, type);
        return jsonResult;
    }



    //修改单个游记收藏状态
    @PutMapping("/changeTravelCollection/{travelId}")
    public JsonResult changeTravelCollection(@PathVariable Long travelId, int type) {
        JsonResult jsonResult = new JsonResult();
        userService.changeTravelCollection(travelId, type);
        return jsonResult;
    }

    //修改单个攻略收藏状态
    @PutMapping("/changeStrategyCollection/{strategyId}")
    public JsonResult changeStrategyCollection(@PathVariable Long strategyId, int type) {
        JsonResult jsonResult = new JsonResult();
        userService.changeStrategyCollection(strategyId, type);
        return jsonResult;
    }

    //查询被访问人的粉丝数
    @GetMapping("/fans/{userId}")
    public int getFansCount(@PathVariable Long userId){
        return userService.getFansCount(userId);
    }

    //查询被访问人的关注数
    @GetMapping("/focus/{fansId}")
    public int getFocusCount(@PathVariable Long fansId){
        return userService.getFocusCount(fansId);
    }
    //取消点赞
    @DeleteMapping("cancelLike/{articleId}/{type}")
    public void cancelLike(@PathVariable Long articleId,@PathVariable Integer type){
        userService.cancelLike(articleId, type);
    }

    //当前用户是否是被访问人的粉丝
    @GetMapping("/beVisited")
    public int judge(Long beVisitedId,Long currentId){
        return userService.judge(beVisitedId,currentId);
    }

    //添加关注
    @PostMapping("/addFocus")
    public JsonResult addFocus(Long beVisitedId,Long currentId){
        JsonResult jsonResult = new JsonResult();
        try {
            userService.addFocus(beVisitedId,currentId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    //取消关注
    @DeleteMapping("{beVisitedId}/deleteFocus/{currentId}")
    public JsonResult deleteFocus(@PathVariable Long beVisitedId,@PathVariable Long currentId){
        JsonResult jsonResult = new JsonResult();
        try {
            userService.deleteFocus(beVisitedId, currentId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //查询当前用户的心愿单
    @GetMapping("/wishes")
    public PageInfo listForWishes(WishQueryObject qo){
        qo.setPageSize(5);
        return userService.listForWishes(qo);
    }

    //添加心愿到当前用户
    @PutMapping("/saveWish/{strategyId}")
    public JsonResult saveWish(@PathVariable Long strategyId ){
        JsonResult jsonResult = new JsonResult();
        try {
            userService.saveWish(strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //当前用户删除心愿
    @DeleteMapping("/deleteWish/{strategyId}")
    public JsonResult deleteWish(@PathVariable Long strategyId ){
        JsonResult jsonResult = new JsonResult();
        try {
            userService.deleteWish(strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    //点赞
    @PostMapping("like/{articleId}/{type}")
    public void like(@PathVariable Long articleId,@PathVariable Integer type){
        userService.like(articleId, type);
    }

    //查询赞我的信息
    @GetMapping("/likeMessage")
    public PageInfo queryLikeMessage(LikeMessageQueryObject qo) {
        //设置排序字段
        qo.setOrderBy("lm.create_time desc");
        //调用业务方法
        PageInfo pageInfo = userService.queryLikeMessage(qo);
        userService.updateLikeMessageState(pageInfo.getList());
        return pageInfo;
    }

    //查询未读的赞我信息
    @GetMapping("/totalLikeNum/{authorId}/unreads")
    public Map queryTotalLikeNum(@PathVariable Long authorId) {
        Map map = new HashMap<>();
        Integer totalLikeNum = userService.queryTotalLikeNum(authorId);
        map.put("totalLikeNum",totalLikeNum);
        //调用业务方法
        return map;
    }

    //查询私信聊天记录
    @GetMapping("/chatMessages")
    public List queryChatMessage(ChatMessageQueryObject qo) {
        //查询所有的历史信息记录
        List<ChatMessage> chatMessages = chatMessageService.selectAllForList(qo);
        //将查过的消息的状态设置为已读
        chatMessageService.updateStatus();
        //返回查询到的数据
        return chatMessages;
    }

    //查询私信聊天记录
    @GetMapping("/chatSet/{userId}")
    public List queryChatList(@PathVariable Long userId) {
        //查询该用户所有的聊天对象
        List<User> chatSet = chatMessageService.selectChatSet(userId);
        //返回查询到的数据
        return chatSet;
    }

    //查询被访问者的游记,点评,心愿数量
    @GetMapping("{beVisitedId}/allNum")
    public int[] coutForAll(@PathVariable Long beVisitedId) {
        return userService.coutForAll(beVisitedId);
    }

    //查询被访问者的游记,点评,心愿列表
    @GetMapping("{beVisitedId}/all/{type}")
    public PageInfo ListByUserIdAndType(@PathVariable Long beVisitedId,@PathVariable int type,QueryObject qo) {
        return userService.ListByUserIdAndType(qo,beVisitedId,type);
    }

    /**
     * 查询当前用户的未读的系统通知总数
     */
    @GetMapping("/totalNoticeNum/{userId}")
    public Map queryTotalNoticeNum(@PathVariable Long userId) {
        Map map = new HashMap<>();
        Integer totalNoticeNum = userService.queryTotalNoticeNum(userId);
        map.put("totalNoticeNum",totalNoticeNum);
        //调用业务方法
        return map;
    }
    /**
     * 查询当前用户的系统通知列表
     */
    @GetMapping("/selectNoticeList/{userId}")
    public PageInfo selectNoticeList(@PathVariable Long userId,QueryObject qo) {
        return userService.selectNoticeList(userId, qo);
    }
    /**
     * 更改当前用户的系统通知状态,更改为已读
     */
    @GetMapping("/updateNoticeState/{userId}")
    public void updateNoticeState(@PathVariable Long userId) {
       userService.updateNoticeState(userId);
    }
    /**
     *查询当前用户未读的通知的id
     */
    @GetMapping("/selectNoticeState/{userId}")
    public Map selectNoticeState(@PathVariable Long userId) {
        return userService.selectNoticeState(userId);
    }
    /**
     *查询当前用户的朋友圈动态
     */
    @GetMapping("/{userId}/dynamics")
    public PageInfo selectDynamic(DynamicQueryObject qo) {
        qo.setOrderBy("d.create_time desc");
        return dynamicService.query(qo);
    }

    @GetMapping("/{userId}/focus")
    public PageInfo ListForFocus(QueryObject qo) {
        return userService.ListForFocus(qo);
    }
    @GetMapping("/{userId}/fans")
    public PageInfo ListForFans(QueryObject qo) {
        return userService.ListForFans(qo);
    }
}
